package com.daluzy.chmanager.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.daluzy.chmanager.MyApplication;
import com.daluzy.chmanager.R;
import com.daluzy.chmanager.activity.StudentManageActivity;
import com.daluzy.chmanager.adapter.AreaAdapter;
import com.daluzy.chmanager.bean.Area;
import com.daluzy.chmanager.bean.School;
import com.daluzy.chmanager.bean.Students;
import com.daluzy.chmanager.bean.TestPoint;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class StudentHomeFragment extends Fragment {

    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.tv_school_name)
    TextView tvSchoolName;
    @BindView(R.id.tv_school_tel)
    TextView tvSchoolTel;
    @BindView(R.id.tv_school_address)
    TextView tvSchoolAddress;
    @BindView(R.id.tv_school_details)
    TextView tvSchoolDetails;
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    private Context context;
    private Students students;
    private School school;
    private LoadingDialog loadingDialog;
    private List<Area> areaList = new ArrayList<>();

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        this.context = getActivity();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_student_home_daluzy, container, false);
        ButterKnife.bind(this, view);
        students = MyApplication.getIntance().getStudents();
        loadingDialog = new LoadingDialog(getContext());
        rvData.setLayoutManager(new LinearLayoutManager(context));
        getSchool();
        return view;
    }

    private void getSchool() {
        if (loadingDialog != null)
            loadingDialog.show();
        BmobQuery<School> categoryBmobQuery = new BmobQuery<>();
        categoryBmobQuery.addWhereEqualTo("school_id", students.getStudent_school());
        categoryBmobQuery.findObjects(new FindListener<School>() {
            @Override
            public void done(List<School> object, BmobException e) {
                if (e == null) {
                    if (object != null && object.size() > 0) {
                        school = object.get(0);
                        setSchoolData();
                    } else {
                        if (loadingDialog != null)
                            loadingDialog.close();
                    }

                } else {
                    Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
                    if (loadingDialog != null)
                        loadingDialog.close();
                }
            }
        });
    }

    private void setSchoolData() {
        tvSchoolAddress.setText(school.getSchool_address());
        tvSchoolDetails.setText(school.getSchool_details());
        tvSchoolName.setText(school.getSchool_name());
        tvSchoolTel.setText(school.getSchool_tel());
        initData();
    }

    private void initData() {
        BmobQuery<Area> query = new BmobQuery<>();
        query.addWhereEqualTo("area_school", school.getSchool_name());
        query.order("-createdAt")
                .findObjects(new FindListener<Area>() {
                    @Override
                    public void done(List<Area> object, BmobException e) {
                        if (e == null) {
                            if (object != null && object.size() > 0) {
                                getTestPoint(object);
                            } else {
                                if (loadingDialog != null)
                                    loadingDialog.close();
                            }
                        } else {
                            if (loadingDialog != null)
                                loadingDialog.close();
                        }
                    }
                });
    }

    public void getTestPoint(List<Area> areas) {
        if (areaList != null)
            areaList.clear();
        for (Area area : areas) {
            BmobQuery<TestPoint> query = new BmobQuery<>();
            query.addWhereEqualTo("area", area.getArea_id());
            query.findObjects(new FindListener<TestPoint>() {
                @Override
                public void done(List<TestPoint> object, BmobException e) {
                    if (e == null) {
                        if (object != null && object.size() > 0) {
                            area.setTestPointList(object);
                        } else {
                            if (loadingDialog != null)
                                loadingDialog.close();
                        }
                    } else {
                        if (loadingDialog != null)
                            loadingDialog.close();
                    }
                    areaList.add(area);
                    if (areaList.size() == areas.size()) {
                        if (loadingDialog != null)
                            loadingDialog.close();
                        AreaAdapter areaAdapter = new AreaAdapter(context, areaList);
                        rvData.setAdapter(areaAdapter);
                    }
                }
            });
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        getSchool();
    }
}
